<?php
/**
 * 抢购活动管理
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Log;
class PActivityController extends Controller
{
    protected $return;
   
    public function __construct(){
        header("Access-Control-Allow-Origin: *");
        $this->return = array('errcode'=>0, 'errmsg'=>'ok');
        $admin = session('rush_admin');
        if (!$admin) {
        //    $this->ajaxReturn(array('errcode'=>10000,'errmsg'=>'请先登录'));
        }
    }

    public function index()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['status'] = I('post.type', -1, 'intval');
        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('page_size', 10, 'intval');

        $map['is_deleted'] = 0;
        if ($data['name'] != '') {
            $map['name'] = array('LIKE', '%'.$data['name'].'%');
        }
        if ($data['status'] != -1) {
            $map['status'] = $data['status'];
        }
        $total = M("PActivity")->where($map)->count();
        $page = pagination($total, $data['page_size'] ,$data['page']);
        if ($total) {
            $list = M("PActivity")->where($map)->order('id desc')
                  ->limit($page['offset'], $page['limit'])
                  ->select();
            foreach ($list as $k => $v) {
                $list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
                if($v['status'] == 1){
                    $list[$k]['statusText'] = '上线';
                }else{
                    $list[$k]['statusText'] = '下线';
                }
                $list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);
                $list[$k]['end_time'] = date('Y-m-d H:i', $v['end_time']);
            }
            $res =  array('data'=>$list, 'page'=>$page);
        }else{
            $res = array('data'=>array(), 'page'=>$page);
        }
        return $this->ajaxReturn(array_merge($this->return, $res));
    }

    public function updateStatus()
    {
        $id = I('post.id', 0, 'intval');
        $status = I('post.status');
        if (!in_array($status, array(0, 1))) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'状态错误'));
        }
        $res = M("PActivity")->where(array('id'=>$id))->setField('status', $status);
        if ($res !== false) {
            return $this->ajaxReturn($this->return);
        }else{
            return $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'更新状态失败'));
        }

    }

    public function add()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['enroll_score'] = I('post.enroll_score', 0, 'intval');
        $data['share_score'] = I('post.share_score', 0, 'intval');
        $data['limit_score'] = I('post.limit_score', 0, 'intval');
        $data['status'] = I('post.status', -1, 'intval');
        $data['start_time'] = I('post.start_time', '', 'trim');
        $data['end_time'] = I('post.end_time', '', 'trim');
        $data['description'] = I('post.description', '', 'trim');
        $data['description'] = htmlspecialchars($data['description']);

        if (empty($data['name'])) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'活动名称不能为空'));
        }
        if (!in_array($data['status'], array(0,1))) {
            return $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'请选择活动状态'));
        }
        if (empty($data['url'])) {
            return $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'活动图片不能为空'));
        }
        if ($data['enroll_score'] <= 0) {
            return $this->ajaxReturn(array('errcode'=>10004, 'errmsg'=>'报名积分必须为正整数'));
        }
        if ($data['share_score'] <= 0) {
            return $this->ajaxReturn(array('errcode'=>10005, 'errmsg'=>'分享积分必须为正整数'));
        }
        if ($data['limit_score'] <= 0) {
            return $this->ajaxReturn(array('errcode'=>10006, 'errmsg'=>'封顶积分必须为正整数'));
        }
        if ($data['description'] == '') {
            return $this->ajaxReturn(array('errcode'=>10007, 'errmsg'=>'活动介绍不能为空'));
        }
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            return $this->ajaxReturn(array('errcode'=>10008, 'errmsg'=>'兑换时间错误'));
        }
        $map['name'] = $data['name'];
        $map['is_deleted'] = 0;
        $count = M("PActivity")->where($map)->count();
        if ($count) {
            return $this->ajaxReturn(array('errcode'=>20001, 'errmsg'=>'活动名称已存在'));
        }
        $data['add_time'] = time();
        $res = M("PActivity")->add($data);
        if ($res) {
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'新增活动成功'));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'新增活动失败'));
        }
    }
        
    public function edit()
    {
        $id = I('post.id', 0, 'intval');
        $res = M("PActivity")->find($id);
        if ($res) {
            $res['description'] = htmlspecialchars_decode($res['description']);
            $res['start_time'] = date('Y-m-d H:i:s', $res['start_time']);
            $res['end_time'] = date('Y-m-d H:i:s', $res['end_time']);
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'ok', 'data'=>$res));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'查找不到对应的礼品数据'));
        }
    }

    public function update()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['enroll_score'] = I('post.enroll_score', 0, 'intval');
        $data['share_score'] = I('post.share_score', 0, 'intval');
        $data['limit_score'] = I('post.limit_score', 0, 'intval');
        $data['status'] = I('post.status', -1, 'intval');
        $data['id'] = I('post.id', 0, 'intval');
        $data['description'] = I('post.description', '', 'trim');
        $data['description'] = htmlspecialchars($data['description']);
        $data['start_time'] = I('post.start_time', '', 'trim');
        $data['end_time'] = I('post.end_time', '', 'trim');
        if (empty($data['name'])) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'活动名称不能为空'));
        }
        if (!in_array($data['status'], array(0,1))) {
            return $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'请选择活动状态'));
        }
        if (empty($data['url'])) {
            return $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'活动图片不能为空'));
        }
        if ($data['enroll_score'] <= 0) {
            return $this->ajaxReturn(array('errcode'=>10004, 'errmsg'=>'报名积分必须为正整数'));
        }
        if ($data['share_score'] <= 0) {
            return $this->ajaxReturn(array('errcode'=>10005, 'errmsg'=>'分享积分必须为正整数'));
        }
        if ($data['limit_score'] <= 0) {
            return $this->ajaxReturn(array('errcode'=>10006, 'errmsg'=>'封顶积分必须为正整数'));
        }
        if ($data['description'] == '') {
            return $this->ajaxReturn(array('errcode'=>10007, 'errmsg'=>'活动介绍不能为空'));
        }

        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            return $this->ajaxReturn(array('errcode'=>10008, 'errmsg'=>'兑换时间错误'));
        }
        $map['id'] = $data['id'];
        $map['is_deleted'] = 0;
        $act = M("PActivity")->where($map)->find();
        if (!$act) {
            return $this->ajaxReturn(array('errcode'=>20001,'errmsg'=>'要修改的活动不存在'));
        }
        unset($map);
        $map['name'] = $data['name'];
        $map['is_deleted'] = 0;
        $act = M("PActivity")->where($map)->find();
        if ($act and $act['id'] != $data['id']) {
            return $this->ajaxReturn(array('errcode'=>20001, 'errmsg'=>'活动名称已存在'));
        }
        $res = M("PActivity")->save($data);
        if ($res !== false) {
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'修改活动成功'));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'修改活动失败'));
        }
    }

    public function upload()
    {
        if(!empty($_FILES)){
            $config = array(
                'exts'=>array('png','jpg','jpeg'),
                'maxSixe' => 1024 * 1024 * 5,
                'rootPath' =>'./Uploads/',
                'savePath' =>'act_img/',
                'replace' => 'true',
                'saveName'=> 'activityPic',
                'subName'  => '',
                'driver' =>'Local'
                );
            $upload = new \Think\Upload($config);
            if(!$info = $upload->upload()){
                $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>$upload->getError()));
            }
            $filename = $upload->rootPath.$info['file']['savepath'].$info['file']['savename'];
            $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
            $dir = $_SERVER['SCRIPT_NAME'];
            if(strpos($dir, '/devzhaoshifu/') !== false){
                $dir = '/devzhaoshifu';
            }elseif(strpos($dir, '/zhaoshifu/') !== false){
                $dir = '/zhaoshifu';
            }else{
                $dir = '';
            }
            $imgurl='http://'.$_SERVER['SERVER_NAME'].$dir."/Uploads/act_img/activityPic.".$extension; 
            $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>$imgurl));

        }else{
            $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'请上传文件'));
        }
    }

}